package com.ruoyi.system.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.ILvjudiService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.IZwryqkService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * zwryqkController
 * 
 * @author ruoyi
 * @date 2021-10-28
 */
@Controller
@RequestMapping("/system/zwryqk")
public class ZwryqkController extends BaseController
{
    private String prefix = "system/zwryqk";

    @Autowired
    private IZwryqkService zwryqkService;

    @Autowired
    private ILvjudiService lvjudiService;

    @RequiresPermissions("system:zwryqk:view")
    @GetMapping()
    public String zwryqk()
    {
        return prefix + "/zwryqk";
    }

    /**
     * 查询zwryqk列表
     */
    @RequiresPermissions("system:zwryqk:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Zwryqk zwryqk)
    {
        startPage();
        List<Zwryqk> list = zwryqkService.selectZwryqkList(zwryqk);
        return getDataTable(list);
    }

    /**
     * 查询zwryqk列表
     */
    //@RequiresPermissions("system:zwryqk:list")
    @PostMapping("/customlist")
    @ResponseBody
    public TableDataInfo customlist(ZwryqkSelect zwryqk)
    {
        startPage();
        List<Map<String,Object>> list = zwryqkService.selectCustomZwryqkList(zwryqk);
        return getDataTable(list);
    }

    /**
     * 导出zwryqk列表
     */
    @RequiresPermissions("system:zwryqk:export")
    @Log(title = "zwryqk", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ZwryqkSelect zwryqk)
    {
        List<ZwryqkCustom> list = zwryqkService.ExportCustom(zwryqk);
        ExcelUtil<ZwryqkCustom> util = new ExcelUtil<ZwryqkCustom>(ZwryqkCustom.class);
        return util.exportExcel(list, "在外人员情况");
    }

    /**
     * 新增zwryqk
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存zwryqk
     */
    //@RequiresPermissions("system:zwryqk:add")
    @Log(title = "zwryqk", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Zwryqk zwryqk,String lvjudi)
    {
        zwryqk.setId(UUID.randomUUID().toString());
        zwryqk.setChuangjiantime(new Date());
        zwryqk.setStatus("1");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<String> list = zwryqkService.selectZwryqkByUserIdAndLiningtime(zwryqk.getUserid(),sdf.format(zwryqk.getLiningtime()));
        if(list.size()>0){
            return error("该离宁时间已上报，如需修改，请删除原有填报");
        }
        return toAjax(zwryqkService.insertZwryqk(zwryqk,lvjudi));
    }

    /**
     * 修改zwryqk
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        Zwryqk zwryqk = zwryqkService.selectZwryqkById(id);

        String ljd = "";
        Lvjudi mmLjd = new Lvjudi();
        mmLjd.setZwryqkid(id);
        List<Lvjudi> mmLjdList = lvjudiService.selectLvjudiList(mmLjd);
        if (mmLjdList.size()==1){
            ljd = mmLjdList.get(0).getLvjudi();
        }

        zwryqk.setText1(ljd);

        mmap.put("zwryqk", zwryqk);
        return prefix + "/edit";
    }

    /**
     * 修改保存zwryqk
     */
    @RequiresPermissions("system:zwryqk:edit")
    @Log(title = "zwryqk", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Zwryqk zwryqk,String lvjudi)
    {
        return toAjax(zwryqkService.updateZwryqk(zwryqk,lvjudi));
    }

    /**
     * 删除zwryqk
     */
    //@RequiresPermissions("system:zwryqk:remove")
    @Log(title = "zwryqk", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(zwryqkService.deleteZwryqkByIds(ids));
    }
}
